﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./CoLoader.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 20;
    public string strPager = "";
    public string strIDs = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public string strAlphaBet = "";
    public string strLetter = "letter";
    string strChar = "";
    string strExcluded = "id";
    public string strPartQuery = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        string strPageID = "page_number";
        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;

        if (Session["coloader"] != null && int.Parse(Session["coloader"].ToString()) > 0)
        {
            nPermissions = int.Parse(Session["coloader"].ToString());
        }
        if (nPermissions < 1)
            Response.Redirect("Login.aspx");
        try
        {
            if (Request.QueryString[strLetter] != null)
            {
                strChar = Request.QueryString[strLetter].ToString();
                strCondition = " AND Carr like '" + strChar + "%'";
            }
        }
        catch
        {
            strChar = "";
        }
        if (!Page.IsPostBack)
        {
            if (nPermissions < 3)
            {
                btnupdate.Visible = false;
                btnAddNew.Visible = false;
            }
            else
            {
                if (nPermissions < 7)
                    btnAddNew.Visible = false;
            }
            CommonFunctions.GetAllQueryString(Request, "", "action;id", out strPartQuery);
            if (Request.QueryString["action"] == "delete" && nPermissions > 7)
            {
                CoLoader obj = new CoLoader();
                obj.ID = long.Parse(Request.QueryString["ID"]);
                obj.Delete(GV.cn, GV.trans);
                Response.Redirect("Coloader.aspx" + strPartQuery);
            }
            else
            {
                if (Request.QueryString["custID"] != null)
                {
                }
                if (Request.QueryString["ID"] != null)
                {
                    //loadCarrContact();
                }
            }
            #region Build List
            int nNumOfPages = 0;

            DataTable dtList;
            BuildList(NumOfItemsPerPage, nPage, out nNumOfPages, out dtList, null, null);
            if (nPage > nNumOfPages) nPage = 1;

            rptData.DataSource = dtList;
            rptData.DataBind();
            #endregion
            #region Build Pager
            CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
            #endregion
            CommonFunctions.BuildAphabetSearch(Request, ContainerPage, strLetter, strChar, strExcluded, out strAlphaBet);
        }

    }
    //protected void loadCarrContact()
    //{
    //    CoLoader obj = new CoLoader();
    //    obj.ID = long.Parse(Request.QueryString["ID"]);
    //    obj.Load(GV.cn, GV.trans);
    //    txtCarrier.Text = HttpUtility.HtmlDecode(obj.Carr);
    //    txtPort.Text = HttpUtility.HtmlDecode(obj.Port);
    //    txtPICSALES.Text = HttpUtility.HtmlDecode(obj.Pic_sales);
    //    txtTel.Text = HttpUtility.HtmlDecode(obj.Tel);
    //    txtCell.Text = HttpUtility.HtmlDecode(obj.Cell);
    //    txtCharge.Text = HttpUtility.HtmlDecode(obj.Charge);
    //    txtSkype.Text = HttpUtility.HtmlDecode(obj.Skype);
    //    txtEmail.Text = HttpUtility.HtmlDecode(obj.Email);
    //}

    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        CoLoader obj = new CoLoader();
        obj.ID = -1;
        if (Request.QueryString["ID"] != null)
        {
            obj.ID = long.Parse(Request.QueryString["ID"]);
            obj.Load(GV.cn, GV.trans);
        }
        try
        {
            obj.Sort = int.Parse(txtSort.Text);
        }
        catch { obj.Sort = 0; }
        obj.Carr = txtCarrier.Text;
        obj.Port = txtPort.Text;
        obj.Pic_sales = txtPICSALES.Text;
        obj.Tel = txtTel.Text;
        obj.Cell = txtCell.Text;
        obj.Charge = txtCharge.Text;
        obj.Skype = txtSkype.Text;
        obj.Email = txtEmail.Text;
        obj.LastUpdate = DateTime.Now;

        if (strError == "")
        {
            obj.Save(GV.cn, GV.trans);
        }
        CommonFunctions.GetAllQueryString(Request, "", "action;id", out strPartQuery);
        Response.Redirect("CoLoader.aspx" + strPartQuery);
    }
    private void BuildList(int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("ID");
        dtList.Columns.Add("Sort");
        dtList.Columns.Add("Index");
        dtList.Columns.Add("Carr");
        dtList.Columns.Add("Port");
        dtList.Columns.Add("Pic_sales");
        dtList.Columns.Add("PicID");
        dtList.Columns.Add("Tel");
        dtList.Columns.Add("Cell");
        dtList.Columns.Add("Charge");
        dtList.Columns.Add("Skype");
        dtList.Columns.Add("Email");
        dtList.Columns.Add("LastUpdate");
        dtList.Columns.Add("Action");

        //Get List by SoIDs
        nNumOfPages = 0;

        if (strCondition == "") strCondition = "All";
        CoLoader obj = new CoLoader();


        CoLoader.GetList(strCondition, "Charge", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

        string strAllQueryString = "";

        foreach (string strKey in Request.QueryString.AllKeys)
        {
            if (strKey != null)
            {
                if ((strKey.ToLower() != "action") && (strKey.ToLower() != "id"))
                    strAllQueryString = strAllQueryString + "&" + strKey + "=" + Request.QueryString[strKey].ToString();
            }
        }

        if (strAllQueryString.Length >= 1)
            strAllQueryString = strAllQueryString.Substring(1);

        if (ds != null)
        {
            int count = 0;
            if (Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "")
            {
                int pagenumber = 1;
                try
                {
                    pagenumber = int.Parse(Request.QueryString["page_number"]);
                    count = NumOfItemsPerPage * (pagenumber - 1);
                }
                catch { count = 0; }
            }
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                count++;
                string[] arrUpdate = new string[0];
                string ID = dr["ID"].ToString();
                string strSTT = dr["Sort"].ToString();
                string strIndex = count.ToString();
                string strCarr = dr["Carr"].ToString();
                string strPort = dr["Port"].ToString();
                string strPic_sales = dr["Pic_sales"].ToString();
                string strPicID = dr["PicID"].ToString();
                string strTel = dr["Tel"].ToString();
                string strCell = dr["Cell"].ToString();
                string strCharge = dr["Charge"].ToString();
                string strSkype = dr["Skype"].ToString();
                string strEmail = dr["Email"].ToString();
                string strLastUpdate = "";
                if (dr["LastUpdate"].ToString() != "") strLastUpdate = DateTime.Parse(dr["LastUpdate"].ToString()).ToString("dd/MM/yyyy");

                string strTitleCarr = "";
                if (strCarr != "")
                {
                    arrUpdate = strCarr.Split('_');
                    if (arrUpdate.Length > 1)
                    {
                        strCarr = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                        strTitleCarr = "title=\"" + arrUpdate[1].ToString() + "\"";
                    }
                    else
                        strCarr = arrUpdate[0].ToString();
                }
                if (strPort != "")
                {
                    arrUpdate = strPort.Split('_');
                    if (arrUpdate.Length > 1)
                        strPort = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strPort = arrUpdate[0].ToString();
                }
                if (strPic_sales != "")
                {
                    arrUpdate = strPic_sales.Split('_');
                    if (arrUpdate.Length > 1)
                        strPic_sales = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strPic_sales = arrUpdate[0].ToString();
                }
                if (strTel != "")
                {
                    arrUpdate = strTel.Split('_');
                    if (arrUpdate.Length > 1)
                        strTel = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strTel = arrUpdate[0].ToString();
                }
                if (strCell != "")
                {
                    arrUpdate = strCell.Split('_');
                    if (arrUpdate.Length > 1)
                        strCell = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strCell = arrUpdate[0].ToString();
                }
                if (strCharge != "")
                {
                    arrUpdate = strCharge.Split('_');
                    if (arrUpdate.Length > 1)
                        strCharge = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strCharge = arrUpdate[0].ToString();
                }
                if (strSkype != "")
                {
                    arrUpdate = strSkype.Split('^');
                    if (arrUpdate.Length > 1)
                        strSkype = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strSkype = arrUpdate[0].ToString();
                }
                if (strEmail != "")
                {
                    arrUpdate = strEmail.Split('^');
                    if (arrUpdate.Length > 1)
                        strEmail = "<span title=\"" + arrUpdate[1].ToString() + "\">" + arrUpdate[0].ToString() + "</span>";
                    else
                        strEmail = arrUpdate[0].ToString();
                }

                string strAction = "";
                string strTemp = "";
                strTemp = dr["Carr"].ToString();
                strTemp = strTemp.Replace("'", "\\'");
                if (nPermissions > 7)
                    strAction += "&nbsp;&nbsp;<a href=\"javascript:doDelete('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "','" + System.Web.HttpUtility.HtmlEncode(strTemp) + "')\">" + "Xóa</a>";
                string[] strValues = new string[] { ID, strSTT, strIndex, strCarr, strPort, strPic_sales, strPicID, strTel, strCell, strCharge, strSkype, strEmail, strLastUpdate, strAction };
                dtList.Rows.Add(strValues);
            }
        }
    }
    public string GetCustomerLink(object id, object picid)
    {
        string url = "/customer.aspx?";
        if (picid != null && id != null)
        {
            url += "picid=" + picid.ToString() + "&id=" + id.ToString();
        }
        else if (picid != null && !string.IsNullOrEmpty(picid.ToString()))
        {
            url += "picid=" + picid.ToString();
        }
        else if (picid == null && id != null)
        {
            url += "id=" + id.ToString();
        }

        return url;
    }
}
